/*
 * psRtc.c
 *
 *  Created on: Jul 16, 2021
 *      Author: tau
 */

#include "psRtc.h"
#include <stdio.h>

#include <sys/ioctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#define RTC_TICK_ON      _IO('p', 0x03)  /* Update int. enable on        */
#define RTC_TICK_OFF     _IO('p', 0x04)  /* ... off                      */
#define RTC_TICK_SET      _IO('p', 0x05)  /* Periodic int. enable on      */
#define RTC_TICK_READ     _IO('p', 0x06)  /* ... off                      */

/*
*setup_time():set up the sec,func
*/
// in rtc_tm, valid values are:
// year 1900-
// month 0-11
// day 1-[28, 29, 30, 31]
// hr 0-23
// min, sec 0 -59

void setup_time(struct rtc_time* rtc){
	int retval,fd;
	fd = open (RTC_DEVICE, O_RDONLY);
	if (fd<0) {
			printf("open rtc faild!!!\n");
			return ;
	}
	retval = ioctl(fd, RTC_SET_TIME, rtc);
	if (retval <0) {
			printf("ioctl RTC_SET_TIME  faild!!!\n");
			close(fd);
			return ;
	}
	printf("Adjust current RTC time test OK!\n");
	close(fd);
}

void read_time(struct rtc_time* rtc)
{
	int retval,fd;
	fd = open (RTC_DEVICE, O_RDONLY);
	if (fd<0) {
			printf("open rtc faild!!!\n");
			return ;
	}
	/*read current time*/
	retval=ioctl(fd,RTC_RD_TIME,rtc);
	if (retval <0) {
		printf("ioctl RTC_RD_TIME  faild!!!\n");
		close(fd);
		return ;
	}
	close(fd);
}
